#include "AdjustFrom.h"
#include "ui_AdjustFrom.h"
#include <QMessageBox>
#include <iostream>
#include <QDebug>




AdjustFrom::AdjustFrom(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AdjustFrom)
{
    ui->setupUi(this);
    radios = new QButtonGroup;
    radios->addButton(ui->rb_len);
    radios->addButton(ui->rb_angle);
    radios->setExclusive(true);
    ui->rb_angle->setChecked(true);
    ui->widget_3->setVisible(false);
    ui->widget_4->setVisible(false);
//    connect(radios,&QButtonGroup::buttonToggled,this,[=](int id, bool checked){

//    });
}

// 获取checkbox索引
int AdjustFrom::getRadioGroupIndex()
{
    return abs(radios->checkedId() + 2 ) ;
}

AdjustFrom::~AdjustFrom()
{
    delete ui;
}


void AdjustFrom::on_pb_CreatChart_clicked()
{
    bool transe = true;
    InputPara inp;

    int curIndex = getRadioGroupIndex();    // 获取角度还是摆幅
    // 如果是摆幅
    if(0==curIndex){
       int len = ui->sp_len->value();
       int arg = ui->sp_Arm->value();
       auto t = lenToArc(len,arg,transe);
       inp.arrPuls = angleToPuls(t);
       inp.backPuls = angleToPuls(t);
    }else{
        auto t = ui->sp_Angle->value();
        inp.arrPuls = angleToPuls(t);
        inp.backPuls = angleToPuls(t);
    }


    // 到达是否匀速运动
    inp.arr_isAverage  = (0 == ui->cb_arr->currentIndex());
    inp.back_isAverage = (0 == ui->cb_back->currentIndex());
    // 滞留等待时间
    inp.sleepTime = ui->le_wait->text().toUInt(&transe);
    // 下次起摆的等待时间
    inp.nextWait = ui->le_next->text().toUInt(&transe);


    if(!transe){
        QMessageBox::warning(NULL, "Title", "输入数值无法转换",
                                 QMessageBox::Yes );
    }
    ui->pb_Gau->setEnabled(true);

    emit creatChart(inp);
}

// 上传到eep
void AdjustFrom::on_pb_EE_clicked()
{
    bool transe = true;

    // 到达是否匀速运动
    inp.arr_isAverage  = (0 == ui->cb_arr->currentIndex());
    inp.back_isAverage = (0 == ui->cb_back->currentIndex());
    // 滞留等待时间
    inp.sleepTime = ui->le_wait->text().toUInt(&transe);
    // 下次起摆的等待时间
    inp.nextWait = ui->le_next->text().toUInt(&transe);
    // 摆动频率
    inp.swingFreq= ui->sp_HZ->value();

    if(!transe){
        QMessageBox::warning(NULL, "Title", "输入数值无法转换",
                                 QMessageBox::Yes );
        return;
    }


    bool wise = inp.arrPuls > 0;

    auto gg = MotorFcs::packageMove(wise,abs(inp.arrPuls),inp.sleepTime,inp.swingFreq);
    int sendByte = 0;

    for(auto f=gg.begin();f!=gg.end();f+=2){
//        u16  x = (*f<<8) + *(f+1);
//        Motor::GetInstance()->sendCMDs(&x);

        /* 这里这样用,现在无休眠还不能用 */
        std::vector<uchar> __cmd;
        __cmd.clear();
        __cmd.push_back(*f);
        __cmd.push_back(*(f+1));
        MotorFcs::printVect(__cmd);
        Motor::GetInstance()->sendCMDs(__cmd);
        sendByte++;
        sleep_ms(10);
    }
    std::cout << "烧录EEPROM共: ["<<sendByte*2<<"]个byte!"<<std::endl;
}

void AdjustFrom::on_pb_Gau_clicked()
{
    emit gauShow();
}

// 摆幅改变立刻触发角度
void AdjustFrom::on_sp_len_editingFinished()
{
    auto arm = ui->sp_Arm->value();
    auto arg1 = ui->sp_len->value();
    auto v = atan2(arg1,arm);   // 获取y/x的弧度
    double Rad_to_deg  = 45.0 / atan(1.0);  // 转换关系
    ui->sp_Angle->setValue(v*Rad_to_deg*10);
}

// 角度改变立刻触发摆幅
void AdjustFrom::on_sp_Angle_editingFinished()
{
    auto arm = ui->sp_Arm->value();
    auto arg1 = ui->sp_Angle->value() * 0.1;
    auto v = tan( (arg1 * atan(1.0) / 45.0) ) * arm ;
    ui->sp_len->setValue(v);
}

// 设置摆幅
void AdjustFrom::on_pb_setLen_clicked()
{
    bool transe;

    int curIndex = getRadioGroupIndex();    // 获取角度还是摆幅
    // 如果是摆幅
    if(0==curIndex){
       int len = ui->sp_len->value();
       int arg = ui->sp_Arm->value();
       auto t = lenToArc(len,arg,transe);
       inp.arrPuls = angleToPuls(t);
    }else{
        auto t = ui->sp_Angle->value();
        inp.arrPuls = angleToPuls(t);
    }

    double max = 1000.0 / (inp.arrPuls*4);
    double min = 1000.0 / (inp.arrPuls*4*2);

    ui->pb_freq->setEnabled(true);
    ui->sp_HZ->setRange(min,max);
    ui->sp_HZ->setValue(max);
    qDebug()<< inp.arrPuls <<max <<min;
}

//设置频率
void AdjustFrom::on_pb_freq_clicked()
{
    inp.swingFreq = ui->sp_HZ->value();
    ui->pb_EE->setEnabled(true);
}
